# coding=utf-8
import os
import sqlite3

import const
from utils import FileUtils


def check_duplicate_file():
    """
    检查文件重复
    :return:
    """
    md5_dict = {}
    file_list = []
    FileUtils.find_file(const.SAVE_ROOT_DIR, file_list, const.DB_NAME)
    for db_file in file_list:
        conn = sqlite3.connect(db_file)
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM ' + const.DB_TABLE)
        row = cursor.fetchone()
        while row is not None:
            md5 = row[0]
            path = os.path.join(os.path.dirname(db_file), row[2])
            if md5 in md5_dict:
                paths = md5_dict[md5]
                paths.append(path)
            else:
                md5_dict[md5] = [path]
            row = cursor.fetchone()
        cursor.close()
        conn.close()
    print('Duplicate File List :')
    for k, v in md5_dict.items():
        if len(v) > 1:
            file_size = os.path.getsize(v[0])
            print(k, ':', FileUtils.format_file_size(file_size), 'x', len(v), '=', FileUtils.format_file_size(file_size * len(v)))
            for path in v:
                print('  -->', path)


def check_backups():
    """
    验证文件是否全部备份
    :return:
    """
    no_backup_list = []
    md5_dict = {}
    FileUtils.find_file_by_md5(const.NEED_BACKUPS_DIR, md5_dict)
    total = len(md5_dict)
    if total:
        conn = sqlite3.connect(os.path.join(const.SAVE_DIR, const.DB_NAME))
        cursor = conn.cursor()
        cursor.execute('CREATE TABLE IF NOT EXISTS ' + const.DB_TABLE + ' ('
                       'md5 CHAR(32) PRIMARY KEY NOT NULL,'
                       'name CHAR(255) NOT NULL,'
                       'path TEXT NOT NULL,'
                       'create_time DATETIME'
                       ')')
        conn.commit()
        print('db is ready')

        for index, (k, v) in enumerate(md5_dict.items()):
            print('check dict', index + 1, '/', total, k, ':', v)
            cursor.execute('SELECT * FROM ' + const.DB_TABLE + ' WHERE md5 = "' + k + '"')
            result = cursor.fetchone()
            if result is None:
                no_backup_list.append((k, v))
        cursor.close()
        conn.close()
        print('db is close')
    print('No Backup File List:')
    for item in no_backup_list:
        print('--->', item)
    print
    print('Total:', total, ',Backup:', total - len(no_backup_list), ',No Backup:', len(no_backup_list))


if __name__ == '__main__':
    check_duplicate_file()

